#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<assert.h>

#include<iostream>
using namespace std;


int main()
{
  pid_t id=fork();
  assert(id>=0);

  if(id==0)
  {
    //子进程
    printf("我是子进程，pid：%d\n",getpid());

    //测试进程替换函数
    char*const myenviron[]={
      "MYENV=youcanseeme",
      NULL 
    };

    //execle("./exec/other_process","other_process",NULL,myenviron);//自己传递给子进程环境变量
    //将自己的变量添加到系统和环境变量中
    extern char** environ;
    putenv("MYENV=youcanseeme");
    execle("./exec/other_process","other_process",NULL,environ);


  
   // execl("/usr/bin/ls","ls","-l","-a",NULL);
   // execlp("ls","ls","-l","-a",NULL);
  
   // char* const argv[]={
   //   "ls",
   //   "-l",
   //   "-a",
   //   NULL
   // };
   //// execv("/usr/bin/ls",argv);
   // execvp("ls",argv);

    
    printf("进程替换失败！\n");

  }
  //父进程
  sleep(1);
  printf("我是父进程，pid：%d\n",getpid());
  int status=0;
  pid_t wait_id=waitpid(id,&status,0);
  printf("child exit code:%d\n",WEXITSTATUS(status));

  return 0;
}
